﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bird : MonoBehaviour
{
    protected bool is_release = false;
    protected bool is_click = false;
    private bool is_fly = false;
    public LineRenderer left_line;
    public LineRenderer right_line;
    public Transform left_pos;
    public Transform right_pos;
    protected Rigidbody2D rb;
    [HideInInspector]
    public SpringJoint2D sj;
    private float max_shot = 2.5f;//float类型要加f
    private TestMyTrail my_trail;
    public GameObject boom;
    private int smooth = 3;
    private SpriteRenderer render;
    public Sprite bird_hurt;
    [HideInInspector]
    public bool can_move = true;
    public AudioClip audio_fly;
    public AudioClip audio_select;
    public AudioClip audio_collision;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        sj = GetComponent<SpringJoint2D>();
        my_trail = GetComponent<TestMyTrail>();
        render = transform.GetComponent<SpriteRenderer>();
    }

    private void OnMouseDown()  //点击该游戏物体时
    {
        if (!is_release && can_move) //对于OnMouse事件额外需要can_fly判断是否暂停
        {
            is_click = true;
            //关闭运动学，避免外力影响鼠标事件
            rb.isKinematic = true;
            AudioPlay(audio_select); //选择音效
        }
    }

    private void OnMouseUp()  //点击该游戏物体弹起时
    {
        if (!is_release && can_move)
        {
            is_release = true;
            is_fly = true;
            is_click = false;
            rb.isKinematic = false;
            left_line.enabled = false;
            right_line.enabled = false;
            //发射后开始拖尾特效
            my_trail.TrailStart();
            AudioPlay(audio_fly); //起飞音效
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (is_release) {
            is_fly = false;
            //一旦碰到其他物体就停止拖尾特效
            my_trail.TrailStop();
            render.sprite = bird_hurt;
            AudioPlay(audio_collision);
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (!is_release)
        {
            //实时绘制弹弓线
            left_line.SetPosition(0, left_pos.position);
            left_line.SetPosition(1, transform.position);
            right_line.SetPosition(0, right_pos.position);
            right_line.SetPosition(1, transform.position);
        }
        if (is_click)
        {
            //屏幕坐标系转换成世界坐标系(两个坐标系原点不同)
            transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            //使操作面与画面在z轴上的差异归0
            transform.position += new Vector3(0,0,-Camera.main.transform.position.z);
            if (Vector3.Distance(transform.position,right_pos.position) > max_shot )
            {
                //单位化向量
                Vector3 temp = (transform.position - right_pos.position).normalized * max_shot;
                transform.position = right_pos.position + temp;
            }
            if (transform.position.y < -1.5) //发射地点限制到地平线
            {
                transform.position = new Vector3(transform.position.x, -2, transform.position.z);
            }
        }
        if (is_release)
        {   //被释放且在飞的情况下可释放技能
            if (is_fly && Input.GetMouseButtonDown(0) && can_move) //获取全局的点击事件
            {
                is_fly = false;
                UseSkill();
            }
            //延迟0.1秒后失去力
            Invoke("StopGiveForce", 0.1f);
            Invoke("BirdDead", 4.9f);
        }
        //实时平滑移动摄像头，跟随小鸟
        Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position,new Vector3(Mathf.Clamp(transform.position.x,0,20),
            Camera.main.transform.position.y, Camera.main.transform.position.z),Time.deltaTime*smooth);
    }

    private void StopGiveForce()
    {
        sj.enabled = false;
    }

    protected virtual void BirdDead()
    {
        Destroy(gameObject);
        Instantiate(boom,transform.position,Quaternion.identity);
        GameManager._instance.birds.Remove(this);
        if (GameManager._instance.NextBird())
        {
            left_line.enabled = true;
            right_line.enabled = true;
        }
    }

    private void AudioPlay(AudioClip clip) //声效音乐播放
    {
        //该音乐播放不受物体销毁的影响
        AudioSource.PlayClipAtPoint(clip,transform.position);
    }

    protected virtual void UseSkill()
    {
    }
}
